/*
 * Copyright 2011 Dominik Pretzsch <dominik.pretzsch at gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.blacksheep.jmoteextract.remote.socket;

import de.blacksheep.jmoteextract.remote.socket.xor.XorSocket;
import java.io.IOException;
import java.io.Serializable;
import java.net.Socket;
import java.rmi.server.RMIClientSocketFactory;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Dominik Pretzsch <dominik.pretzsch at gmail.com>
 */
public class ClientSocketFactory implements RMIClientSocketFactory, Serializable {

    private byte pattern;
    
    private static final Logger LOGGER =  Logger.getLogger(ClientSocketFactory.class.getName());

    public ClientSocketFactory(byte pattern) {
	this.pattern = pattern;
    }
    
    public Socket createSocket(String host, int port)
	throws IOException
    {
        LOGGER.log(Level.FINE, "Creating socket on {0}:{1} with {2}", new Object[]{host, port, pattern});
	return new XorSocket(host, port, pattern);
    }
    
    @Override
    public int hashCode() {
        LOGGER.log(Level.FINE, "Getting hashCode");
	return (int) pattern;
    }

    @Override
    public boolean equals(Object obj) {
        LOGGER.log(Level.FINE, "Comparing...");
	return (getClass() == obj.getClass() &&
		pattern == ((ClientSocketFactory) obj).pattern);
    }    
}
